package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;
    @Transactional
    @Override
    public void addOrder(Orders orders, Long userId) {
        //1.根据传递过来的默认地址id获取地址对象的AddressBook
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        //2.根据传递过来的用户ID获取购物车List<ShoppingCart>
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAllByUserId(userId);
        //根据登录用户的Id获取登录对象
        User user = userMapper.findById(userId);

        //3.完善订单对象Order数据
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        orders.setId(oid);
        orders.setNumber(oid+"");
        orders.setStatus(1);//1代付款
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());//下单时间
        orders.setCheckoutTime(LocalDateTime.now());//结账时间

        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());//登录用户的别名
        orders.setConsignee(addressBook.getConsignee());//收件人

        //4完善订单明细对象List<OrderDetail>数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        //定义总金额
        BigDecimal total = new BigDecimal(0);
        //遍历购物车集合数据 封装每个OrderDetail对象
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //将每个购物善品数据shoppingCart封装到OrderDetail对象里
            OrderDetail orderDetail = new OrderDetail();
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail.setId(orderDetailId);
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());

            //将封装好的orderDetail对象加入orderDetailList
            orderDetailList.add(orderDetail);

            //累加每个商品的金额=数量*单价
            //multiply() 这个是decimal类型的乘法方案
            //add() 这个是decimal类型的加法方案

            total=total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()+"")));

            //注意:完善订单主表的总价
            orders.setAmount(total);

            //5.调用Mapper 新增订单主表数据
            orderMapper.save(orders);
            //6.调用mapper新增多条订单明细表
            orderDetailMapper.saveBatch(orderDetailList);
            //7下单成功后清空购物车
            shoppingCartMapper.deleteByUserId(userId);




        }



    }
}
